<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="考试类型" width="180" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.type }}</span>
        </template>
      </el-table-column>

      <el-table-column label="科目" width="180" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.subject }}</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="开始时间" width="180" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{
              new Date(scope.row.start_time).toLocaleString()
            }}</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="结束时间" width="180" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{
              new Date(scope.row.end_time).toLocaleString()
            }}</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="180" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.status }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >痛みを感じよう
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @next-click="next"
      @prev-click="prev"
      :current-page="currentPage"
      :page-sizes="[8]"
      :page-size="size"
      layout="total, sizes, prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import axios from "axios";
import commons from "../../assets/commons";
export default {
  data() {
    return {
      uid: 0,
      currentPage: 1, //当前页
      total: 0, //总条数
      totalPage: 0, //总页数
      size: 8, //每页大小
      tableData: []
    };
  },
  methods: {
    //开始痛苦(做作业的那个按钮)
    handleEdit(index, row) {

      if (
        new Date().toLocaleString() <
        new Date(row.expiration_time).toLocaleString()
      ) {
        this.$store.commit("setHomeortestid", row.id);
        this.$router.push("/student/exam");
      } else {
        alert("已过期");
      }
    },
    //第一次进入调用的方法
    findtestPage() {
      axios
        .get(
          commons.baseUrl +
            "student/testPage?c_id=" +
            this.$store.state.c_id +
            "&page=" +
            this.currentPage +
            "&size=" +
            this.size
        )
        .then(res => {
          this.tableData = res.data.data;
          console.log(res);
          this.total = res.data.total;
          this.totalPage = res.data.totalPage;
          this.currentPage = res.data.currentPage;
          this.size = res.data.size;
        });
    },
    //下一页
    next() {
      this.currentPage = this.currentPage + 1;
      axios
        .get(
          commons.baseUrl +
            "student/testPage?c_id=" +
            this.$store.state.c_id +
            "&page=" +
            this.currentPage +
            "&size=" +
            this.size
        )
        .then(res => {
          this.tableData = res.data.data;
          console.log(res);
          this.total = res.data.total;
          this.totalPage = res.data.totalPage;
          this.currentPage = res.data.currentPage;
          this.size = res.data.size;
        });
    },

    //上一页
    prev() {
      this.currentPage = this.currentPage - 1;
      axios
        .get(
          commons.baseUrl +
            "student/testPage?c_id=" +
            this.$store.state.c_id +
            "&page=" +
            this.currentPage +
            "&size=" +
            this.size
        )
        .then(res => {
          this.tableData = res.data.data;
          console.log(res);
          this.total = res.data.total;
          this.totalPage = res.data.totalPage;
          this.currentPage = res.data.currentPage;
          this.size = res.data.size;
        });
    }
  },
  mounted() {
    //axios  get请求
    //调用方法获取第一页数据
    this.findtestPage();
  }
};
</script>
<style scoped></style>
